package bankteam.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import bankteam.interfaces.bastanteoInterface;
import bankteam.modelo.Bastanteo;
import bankteam.util.ConexionBD;

public class bastanteoDAO implements bastanteoInterface{

	@Override
	public void registraBastanteo(Bastanteo basta) throws Exception {

		String query = "INSERT INTO bastanteo(Fec_Creacion,Cliente_Cod_Cliente) VALUES (?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, basta.getFechaCreacion());
			stmt.setDouble(2, basta.getCliente().getCodCliente());
			
			int i = stmt.executeUpdate();
			
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			System.out.println(e.getMessage());
		} 
		
	}

	@Override
	public void eliminaBastanteo(int codBastanteo) throws Exception {
		String query = "DELETE FROM Bastanteo where Cod = ?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setDouble(1, codBastanteo);
			
			int i = stmt.executeUpdate();
			
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			System.out.println(e.getMessage());
		} 
		
	}

	@Override
	public void modificaBastanteo(Bastanteo basta) throws Exception {
		String query = "UPDATE SET Fec_Creacion=?,Cliente_Cod_Cliente=? where Cod_Bastanteo=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, basta.getFechaCreacion());
			stmt.setDouble(2, basta.getCliente().getCodCliente());
			stmt.setDouble(3, basta.getCodBastanteo());
			
			int i = stmt.executeUpdate();
			
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			System.out.println(e.getMessage());
		} 
		
	}

}
